package cn.wit.tech.service.impl;

import cn.wit.tech.condition.SearchCurriculumCondition;
import cn.wit.tech.mapper.CurriculumMapper;
import cn.wit.tech.mapper.TechPointMapper;
import cn.wit.utils.pojo.Curriculum;
import cn.wit.utils.pojo.TechPoint;
import cn.wit.tech.service.CurriculumService;
import cn.wit.tech.vo.CurriculumVO;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CurriculumServiceImpl implements CurriculumService {
    @Autowired
    private CurriculumMapper curriculumMapper;
    @Autowired
    private TechPointMapper techPointMapper;

    @Override
    public Result insert(Curriculum curriculum) {
        int i = curriculumMapper.insert(curriculum);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
            int i = curriculumMapper.deleteById(id);
            if(i > 0){
                return Result.ok();
            }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(Curriculum curriculum) {
        int i = curriculumMapper.updateById(curriculum);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @Override
    public Result searchPage(IPage<Curriculum> page, SearchCurriculumCondition condition) {
        QueryWrapper<Curriculum> wrapper3 = new QueryWrapper<>();
        if(condition != null) {
            if(condition.getCurriculumName() != null && condition.getCurriculumName() != "") {
                wrapper3.eq("curriculum_Name", condition.getCurriculumName());
            }
            if(condition.getTechPointName() != null && condition.getTechPointName() != "") {
                QueryWrapper<TechPoint> wrapper = new QueryWrapper<>();
                wrapper.eq("techpoint_Name", condition.getTechPointName());
                TechPoint techPoint = techPointMapper.selectOne(wrapper);
                if(techPoint != null) {
                    wrapper3.eq("techpoint_ID", techPoint.getTechPointId());
                } else {
                    wrapper.eq("techpoint_ID", 0);
                }
            }
        }
        IPage<Curriculum> curriculumIPage = curriculumMapper.selectPage(page, wrapper3);

        List<CurriculumVO> list = new ArrayList<>();
        for(Curriculum cc : curriculumIPage.getRecords()) {
            CurriculumVO vo = new CurriculumVO();
            TechPoint techPoint = techPointMapper.selectById(cc.getTechPointId());
            BeanUtils.copyProperties(cc, vo);
            vo.setTechPoint(techPoint.getTechPointName());
            list.add(vo);
        }
        Page<CurriculumVO> voPage = new Page<>(curriculumIPage.getCurrent(), curriculumIPage.getSize(), curriculumIPage.getTotal(), true);
        voPage.setRecords(list);
        return Result.ok(voPage);
    }

}
